import {_decorator, Component} from 'cc';
import {AudioManager} from "db://assets/Scripts/Common/AudioManager";
import {AudioPath} from "db://assets/Scripts/Common/AudioPath";
import {EventManager} from "db://assets/Scripts/Common/EventManager";
import {EventType} from "db://assets/Scripts/Common/EventType";

const {ccclass, property} = _decorator;

/**
 * 背景音乐控制器
 */
@ccclass('BGMController')
export class BGMController extends Component {

    onEnable() {
        // 注册重新开始游戏事件
        EventManager.on(EventType.RESTART, this.onRestart, this);
    }

    onDisable() {
        // 取消注册重新开始游戏事件
        EventManager.off(EventType.RESTART, this.onRestart, this);
    }

    start() {
        // 播放背景音乐
        AudioManager.getInstance().playMusic(AudioPath.backgroundMusic, true);
    }

    update(deltaTime: number) {

    }

    private onRestart() {
        // 播放背景音乐
        AudioManager.getInstance().playMusic(AudioPath.backgroundMusic, true);
    }
}